package com.jyserver.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author :ChenYM
 * @Date : 2018/10/9 20:10
 * @Version 1.0
 * @Description :时间工具集
 **/
public class JyTime {

    public static String date() {
        /**
         *@Description :获得当前日期
         */
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return df.format(new Date()).toString();
    }

    public static String datetime(String pattern) {
        /**
         *@Description :根据格式获得当前时间
         */
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        return df.format(new Date()).toString();
    }

    public static String time() {
        /**
         *@Description :根据格式（yyyy-MM-dd HH:mm:ss）获得当前时间
         */
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(new Date()).toString();
    }

    public static Boolean isDateBefore(String time) throws ParseException {
        /**
         *@Description :判断当前时间是否在时间time之前
         */
        Date NowDate = new Date();
        DateFormat df = DateFormat.getDateTimeInstance();
        return NowDate.before(df.parse(time));
    }

    public static Boolean isDateBefore(Date time) {
        /**
         *@Description :判断时间是否在时间time之前
         */
        Date NowDate = new Date();
        return NowDate.before(time);
    }

    public static Boolean isDateBefore(String time1, String time2) throws ParseException {
        /**
         *@Description :判断时间time2是否在time1之前
         */
        DateFormat df = DateFormat.getDateTimeInstance();
        return df.parse(time1).before(df.parse(time2));
    }

    public static Boolean isDateBefore(Date time1, Date time2) {
        /**
         *@Description :判断时间time2是否在time1之前
         */
        return time1.before(time2);
    }

    public static Boolean isTimeBefore(String time) throws ParseException {
        /**
         *@Description :判断当前时间是否在时间time之前
         */
        Date NowDate = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return NowDate.before(df.parse(date() + " " + time));
    }

    public static Boolean isTimeBefore(String time1, String time2) throws ParseException {
        /**
         *@Description :判断时间time2是否在time1之前
         */
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.parse(date() + " " + time1).before(df.parse(date() + " " + time2));
    }

    public static Boolean isTimeBefore(Date time1, String time2) throws ParseException {
        /**
         *@Description :判断时间time2是否在time1之前
         */
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return time1.before(df.parse(date() + " " + time2));
    }


    public static Boolean isDateAmong(String time1, String time2) throws ParseException {
        /**
         *@Description :判断时间time2是否在time1之前
         */
        Boolean a = isTimeBefore(time1);
        Boolean b = isTimeBefore(time2);
        if ((a && b) || (!a && !b)) {
            return false;
        } else {
            return true;
        }
    }

    public static Boolean isTimeAmong(String time1, String time2) throws ParseException {
        /**
         *@Description :判断时间time2是否在time1之前
         */
        Boolean a = isTimeBefore(time1);
        Boolean b = isTimeBefore(time2);
        if ((a && b) || (!a && !b)) {
            return false;
        } else {
            return true;
        }
    }

    public static Boolean isDateAmong(Date time1, String time2) throws ParseException {
        /**
         *@Description :判断时间time2是否在time1之前
         */
        Boolean a = isDateBefore(time1);
        Boolean b = isTimeBefore(time2);
        if ((a && b) || (!a && !b)) {
            return false;
        } else {
            return true;
        }
    }

    public static String isWeek(Date time) throws ParseException {
        /**
         *@Description :判断时间是星期几
         */
        SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
        return dateFm.format(time);
    }

    public static String isWeek(String time) throws ParseException {
        /**
         *@Description :判断时间是星期几
         */
        Date date = null;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            time = StampToTime(time);
        } catch (Exception e) {
        }
        try {
            date = df.parse(time);
        } catch (ParseException e) {
            df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = df.parse(time);
        }
        df = new SimpleDateFormat("EEEE");
        return df.format(date);
    }

    public static String isWeek() throws ParseException {
        /**
         *@Description :判断当前时间是星期几
         */
        Date NowDate = new Date();
        SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
        return dateFm.format(NowDate);
    }

    public static String isMonth(Date time) throws ParseException {
        /**
         *@Description :判断时间是几月
         */
        SimpleDateFormat dateFm = new SimpleDateFormat("MM");
        return dateFm.format(time);
    }


    public static String isMonth(String time) throws ParseException {
        /**
         *@Description :判断时间是几月
         */
        Date date = null;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            time = TimeToStamp(time);
        } catch (Exception e) {

        }
        try {
            date = df.parse(time);
        } catch (ParseException e) {
            df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = df.parse(time);
        }
        df = new SimpleDateFormat("MM");
        return df.format(date);
    }

    public static String isMonth() throws ParseException {
        /**
         *@Description :判断当前时间是几月
         */
        Date NowDate = new Date();
        SimpleDateFormat dateFm = new SimpleDateFormat("MM");
        return dateFm.format(NowDate);
    }

    public static String TimeToStamp(String s) throws ParseException {
        /**
         *@Description :将时间转为时间戳
         */
        if (s.indexOf("-") == -1 &&s.indexOf(":") == -1) {
            return s;
        }else if (s.indexOf("-") == -1){
            s = date() + " " + s;
        }
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime()/1000;
        res = String.valueOf(ts);
        return res;
    }

    public static String DateToStamp(String s) throws ParseException {
        /**
         *@Description :将时间转为时间戳
         */
        if (s.indexOf("-") == -1 &&s.indexOf(":") == -1) {
            return s;
        }else if (s.indexOf("-") == -1){
            s = date() + " " + s;
        }
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime()/1000;
        res = String.valueOf(ts);
        return res;
    }


    public static String StampToTime(String Stamp) throws ParseException {
        /**
         *@Description :将时间戳转为时间
         */
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(Stamp);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    public static String StampToDate(long Stamp) throws ParseException {
        /**
         *@Description :将时间戳转为日期
         */

        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        long lt = new Long(Stamp);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    public static Date StampToDate(String Stamp) {
        /**
         *@Description :将时间戳转为时间
         */
        long lt = new Long(Stamp);
        return  new Date(lt);
    }

    public static String TimeToStamp(Date date) throws ParseException {
        /**
         *@Description :将时间转为时间戳
         */
        String res;
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }

    public static String getStamp() {
        /**
         *@Description :获得当前时间的时间戳
         */
        String res;
        Date NowDate = new Date();
        long ts = NowDate.getTime();
        res = String.valueOf(ts);
        return res;
    }

    public static long getStampLong() {
        /**
         *@Description :获得当前时间的时间戳
         */
        Date NowDate = new Date();
        return NowDate.getTime();
    }

    public static Integer weekNumber() {
        /**
         *@Description :获得当前时间的位于当年的第几周
         */
        Date NowDate = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(NowDate);
        return calendar.get(Calendar.WEEK_OF_YEAR);
    }

    public static Integer weekNumber(String today ) {
        /**
         *@Description :获得时间的位于当年的第几周
         */
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(today);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(date);
        return calendar.get(Calendar.WEEK_OF_YEAR);
    }
}